package com.zygh.dlqwservice.module.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.dlqwservice.common.constant.EmployeeResponseCodeConst;
import com.zygh.dlqwservice.common.en.EmployeeStatusEnum;
import com.zygh.dlqwservice.common.pcdomain.ResponseDTO;
import com.zygh.dlqwservice.config.token.RequestTokenBO;
import com.zygh.dlqwservice.constant.CommonConst;
import com.zygh.dlqwservice.module.entity.Dept;
import com.zygh.dlqwservice.module.entity.SysUser;
import com.zygh.dlqwservice.module.entity.vo.SysUserUpdatePwdVO;
import com.zygh.dlqwservice.module.mapper.DeptMapper;
import com.zygh.dlqwservice.module.mapper.SysUserMapper;
import com.zygh.dlqwservice.module.service.ISysUserService;
import com.zygh.dlqwservice.util.DlqwDigestUtil;
import com.zygh.dlqwservice.util.DlqwVerificationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lee
 * @since 2020-12-24
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    private static final String RESET_PASSWORD = "123456";

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private DeptMapper deptMapper;

    /**
     * 查询所有员工
     *
     * @return
     */
    @Override
    public ResponseDTO<PageInfo<SysUser>> findSysUserAll(Integer current, Integer size) {
        PageHelper.startPage(current, size);
        List<SysUser> sysUsers = sysUserMapper.selectSysUserList();
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUsers);
        return ResponseDTO.succData(pageInfo);
    }

    /**
     * 新增员工
     *
     * @param sysUser
     * @return
     */
    @Override
    public ResponseDTO addSysUser(SysUser sysUser) {
        if (StringUtils.isNotEmpty(sysUser.getIdcardno())) {
            boolean checkResult = Pattern.matches(DlqwVerificationUtil.ID_CARD, sysUser.getIdcardno());
            if (!checkResult) {
                return ResponseDTO.wrap(EmployeeResponseCodeConst.ID_CARD_ERROR);
            }
        }

        //同登录名民警
        SysUser sameNameSysUser = sysUserMapper.getByLoginName(sysUser.getUsername(), EmployeeStatusEnum.NORMAL.getValue());
        if (null != sameNameSysUser) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.LOGIN_NAME_EXISTS);
        }
        //同电话民警
        SysUser samePhoneSysUser = sysUserMapper.getByPhone(sysUser.getPhone(), EmployeeStatusEnum.NORMAL.getValue());
        if (null != samePhoneSysUser) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.PHONE_EXISTS);
        }
        //部门信息不存在
        String departmentId = sysUser.getDeptId();
        Dept dept = deptMapper.selectById(departmentId);
        if (dept == null) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.DEPT_NOT_EXIST);
        }
        //同警号
        SysUser sameWarningsignalSysUser = sysUserMapper.getByWarningsignal(sysUser.getWarningsignal(), EmployeeStatusEnum.NORMAL.getValue());
        if (null != sameWarningsignalSysUser) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.WARNINGSIGNAL_EXISTS);
        }

        //如果没有密码  默认设置为123456
        String pwd = sysUser.getPassword();
        if (StringUtils.isEmpty(pwd)) {
            sysUser.setPassword(DlqwDigestUtil.encryptPassword(CommonConst.Password.SALT_FORMAT, RESET_PASSWORD));
        } else {
            sysUser.setPassword(DlqwDigestUtil.encryptPassword(CommonConst.Password.SALT_FORMAT, sysUser.getPassword()));
        }
        sysUserMapper.insert(sysUser);
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO<String> updatePwd(SysUserUpdatePwdVO updatePwd, RequestTokenBO requestToken) {
        int requestUserId = requestToken.getRequestUserId();
        SysUser sysUser = sysUserMapper.selectById(requestUserId);
        if (sysUser == null) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.EMP_NOT_EXISTS);
        }
        if (!sysUser.getPassword().equals(DlqwDigestUtil.encryptPassword(CommonConst.Password.SALT_FORMAT, updatePwd.getOldPwd()))) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.PASSWORD_ERROR);
        }
        sysUser.setPassword(DlqwDigestUtil.encryptPassword(CommonConst.Password.SALT_FORMAT, updatePwd.getPwd()));
        sysUserMapper.updateById(sysUser);
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO<List<SysUser>> findSysUserByid(String id) {
        List<SysUser> sysUserByid = sysUserMapper.findSysUserByid(id);
        if (sysUserByid == null) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.EMPTY_INFORMATION_ERROR);
        }
        return ResponseDTO.succData(sysUserByid);
    }

    @Override
    public ResponseDTO<List<SysUser>> findSysUserByDeptid(String id) {
        List<SysUser> sysUserByDeptid = sysUserMapper.findSysUserByDeptid(id);
        if (sysUserByDeptid == null) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.EMPTY_INFORMATION_ERROR);
        }
        return ResponseDTO.succData(sysUserByDeptid);
    }

    @Override
    public ResponseDTO<SysUser> findByWarningsignal(String warningsignal) {
        SysUser byWarningsignal = sysUserMapper.findByWarningsignal(warningsignal);
        if (byWarningsignal == null) {
            return ResponseDTO.wrap(EmployeeResponseCodeConst.EMPTY_INFORMATION_ERROR);
        }
        return ResponseDTO.succData(byWarningsignal);
    }


}
